<?php
require_once('GraphIndex.class.php');
/**
 * Triples - a class for converting arrays of triples into other data structures
 *
 * @package converters
 * @author Keith Alexander
 **/
class Triples extends ConvertableObject
{
	/**
	 * value - an associative array of triples, 
	 * as returned by the ARC RDF/XML parser
	 *
	 * @var array
	 **/
	var $value = array();
	
	/**
	 * Triples::to_resources()
	 * returns a GraphIndex object
	 * @param array $triples
	 * @return object
	 * @author Keith Alexander
	 **/
	public function to_resources($triples=false)
	{	
		if(!$triples) $triples = $this->value();
		// this is the rdf/php data object
		$resources = array();
		$ns_no = 1;
		foreach($triples as $t)
		{
			$o = array();
			$o['value'] = $t['o'];
			
			if($t['o_type']=='uri' OR $t['o_type']=='iri')
			{
				$o['type'] = 'uri';
			}
			elseif($t['o_type']=='literal' OR $t['o_type']=='literal2' OR $t['o_type']=='literal1')
			{
				$o['type'] = 'literal';
			}
			else
			{
				$o['type'] = 'bnode';
			}
			
			$s = $t['s'];
			
			$p = $t['p'];
			if(!empty($s))
			{
				if(!empty($t['o_lang'])) $o['lang'] = $t['o_lang'];
				if(!empty($t['o_dt'])) $o['datatype'] = $t['o_dt'];
				if(!isset($resources[$s]))$resources[$s]=array();
				if(!isset($resources[$s][$p]))$resources[$s][$p]=array();
				if(!in_array($o, $resources[$s][$p])) $resources[$s][$p][]=$o;
			}
		}
		
		$Resources = new GraphIndex($resources, $this->containerObject);
		$Resources->set_value($resources);	
		return $Resources;
	}


}

?>